#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <semaphore.h>

using namespace std;

sem_t  sem1;
sem_t  sem2;

void*a_fn(void*arg){

    sem_wait(&sem1);//减一小于0
    std::cout<<"a running"<<std::endl;
    return (void*)0;
}


void*b_fn(void*arg){
    sem_wait(&sem2);
    std::cout<<"b running"<<std::endl;
    sem_post(&sem1);
    return (void*)0;
}


void*c_fn(void*arg){
    
    std::cout<<"c running"<<std::endl;
    //释放线程b
    sem_post(&sem2);
    return (void*)0;
}


int main(){

    pthread_t a,b,c;

    //信号量初始化，初始值为0
    sem_init(&sem1,0,0);
    sem_init(&sem2,0,0);
    
    pthread_create(&a,NULL,a_fn,(void*)0);
    pthread_create(&b,NULL,b_fn,(void*)0);
    pthread_create(&c,NULL,c_fn,(void*)0);


    //销毁

    pthread_join(a,NULL);
    pthread_join(b,NULL);
    pthread_join(c,NULL);











    return 0;
}